"
I am EndianDetector, a class responsible of detecting the endianess in the current machine.

I do so in my #calcEndianess method, which works as follows:

 - it creates a float object, whose endianess is managed automatically by the VM.
 - copy the raw data of that float object into a bytes object (instance of my class)
 - then, we can check in the raw copy if the data of the float is in the least or more significant word of the object.

Internally, I do the raw data copy by changing the class of the float into me. To do that I have the following properties:
- I'm a compact class (required to change the class)
- I'm in the startup list to check this on every startup
"
Class {
	#name : 'EndianDetector',
	#superclass : 'Object',
	#type : 'words',
	#classVars : [
		'EndianCache'
	],
	#category : 'Graphics-Primitives-Utilities',
	#package : 'Graphics-Primitives',
	#tag : 'Utilities'
}

{ #category : 'private' }
EndianDetector class >> calcEndianness [
	"Store the least significant word of the float with 1; on big endian machines
	 this is the second word"

	| float |
	float := Float new: 2.
	float at: 2 put: 1.
	self adoptInstance: float.
	^ (float at: 1) = 1
		  ifTrue: [ #little ]
		  ifFalse: [ #big ]
]

{ #category : 'accessing' }
EndianDetector class >> endianness [

	^ EndianCache ifNil: [ EndianCache := self calcEndianness ]
]

{ #category : 'class initialization' }
EndianDetector class >> initialize [

	SessionManager default registerSystemClassNamed: #EndianDetector
]

{ #category : 'testing' }
EndianDetector class >> isBigEndian [

	^ self isLittleEndian not
]

{ #category : 'testing' }
EndianDetector class >> isLittleEndian [

	^ self endianness == #little
]

{ #category : 'system startup' }
EndianDetector class >> startUp: isImageStarting [

	isImageStarting ifFalse: [ ^ self ].
	EndianCache := nil
]
